#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.Properties;
using FI.Core.Utilities;
using System;
using System.Configuration;

namespace FI.Core
{
    /// <summary>
    /// Controls to throw configuration excpetion with same way and same settings. 
    /// </summary>
    public static class ExceptionHelper
    {
        #region Constants
        private const string ApplicationNameNotExists = "ApplicationNameNotExists";
        private const string ConcurrentUpdated = "ConcurrentUpdated";
        #endregion

        #region Public Methods
        /// <summary>
        /// Throws the configuration errors exception.
        /// </summary>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <param name="innerException">The inner exception.</param>
        /// <returns></returns>
        public static string ThrowConfigurationErrorsException(string exceptionMessage, Exception innerException)
        {
            throw new ConfigurationErrorsException(exceptionMessage, innerException);
        }

        /// <summary>
        /// Throws the configuration errors exception.
        /// </summary>
        /// <param name="exceptionMessage">The exception message.</param>
        /// <returns></returns>
        public static string ThrowConfigurationErrorsException(string exceptionMessage)
        {
            throw new ConfigurationErrorsException(exceptionMessage);
        }

        /// <summary>
        /// Handle common db exception
        /// </summary>
        /// <param name="ex">The <see cref="Exception"/> to handle.</param>
        public static void HandleCommonDbException(Exception ex)
        {
            Guard.ArgumentNotNull(ex, "ex");
            if (ex.Message.IndexOf(ApplicationNameNotExists, StringComparison.Ordinal) >= 0)
            {
                throw new ConfigurationErrorsException(Resources.ApplicationNameNotExists, ex);
            }

            if (ex.Message.IndexOf(ConcurrentUpdated, StringComparison.Ordinal) >= 0)
            {
                throw new ConcurrentException(Resources.ConcurrentUpdated, ex);
            }
        }
        #endregion
    }
}